package com.lizheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lizheng.constants.SystemConstants;
import com.lizheng.domain.ResponseResult;
import com.lizheng.domain.entity.Comment;
import com.lizheng.domain.vo.CommentVo;
import com.lizheng.domain.vo.PageVo;
import com.lizheng.enums.AppHttpCodeEnum;
import com.lizheng.exception.SystemException;
import com.lizheng.mapper.CommentMapper;
import com.lizheng.service.CommentService;
import com.lizheng.service.UserService;
import com.lizheng.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author lizheng
 * @since 2022-07-03 22:13:25
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    @Resource
    private UserService userService;


    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //查询对应文章的根评论
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        //对articleId 进行判断
        wrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId)
        //根评论 rootId 应为1
                .eq(Comment::getRootId,-1)
        //评论类型
                .eq(Comment::getType,commentType);
        //分页查询
        Page<Comment> page = new Page(pageNum,pageSize);
        page(page,wrapper);
        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());
        //查询所有根评论对应的子评论集合，并且赋值给对应的属性
        for (CommentVo commentVo : commentVoList) {
            //查询对应的子评论
            List<CommentVo> children = getChildren(commentVo.getId());
            commentVo.setChildren(children);
        }


        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        //评论内容不能为空
        if(StringUtils.isBlank(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }

    /**
     * 此方法是为comment到commentVo的转换
     * */
    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        //遍历集合
        for (CommentVo commentVo : commentVos) {
            //通过createBy查询此条评论的所属人并赋值
            commentVo.setUsername(userService.getById(commentVo.getCreateBy()).getNickName());
            //通过toCommentUserId查询向谁回复的昵称并赋值
            //判断toCommentUserId不为-1才能进行查询
            if(commentVo.getToCommentUserId()!=-1){
                commentVo.setToCommentUserName(userService.getById(commentVo.getToCommentUserId()).getNickName());
            }
        }
        return commentVos;
    }

    /**
     *根据根评论的id查询所对应的子评论的集合
     * @param id 根评论的id
     * @return
     * */
    private List<CommentVo> getChildren(Long id){
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getRootId,id);
        wrapper.orderByDesc(Comment::getCreateTime);
        List<Comment> comments = list(wrapper);
        return toCommentVoList(comments);
    }
}

